package com.tuanzi.loan.business.repository.galaxycard.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanRole.LOAN_ROLE;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanSystemUser.LOAN_SYSTEM_USER;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanUserRole.LOAN_USER_ROLE;

import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.galaxycard.GalaxyCardPromoteChannelUserRightRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.galaxycard.GalaxyCardPromoteChannelUserRightRequest;
import com.tuanzi.loan.vo.response.galaxycard.GalaxyCardPromoteChannelUserRightResponse;

@SuppressWarnings({ "rawtypes" })
@Repository
public class GalaxyCardPromoteChannelUserRightRepositoryImpl extends JOOQBaseRepository implements GalaxyCardPromoteChannelUserRightRepository{

	@Override
	protected TableImpl getTable() {
		return LOAN_ROLE;
	}

	@Override
	public Page<GalaxyCardPromoteChannelUserRightResponse> list(
			GalaxyCardPromoteChannelUserRightRequest searchRequest, Pageable pageable) {
		
		
		fields(LOAN_ROLE.NAME, LOAN_SYSTEM_USER.NAME,LOAN_SYSTEM_USER.ID);
		
		leftJoin(LOAN_USER_ROLE, LOAN_ROLE.ID.eq(LOAN_USER_ROLE.ROLE_ID));
		leftJoin(LOAN_SYSTEM_USER, LOAN_USER_ROLE.USER_ID.eq(LOAN_SYSTEM_USER.ID));
		
		eq(LOAN_ROLE.CODE, "GALAXYCARDPARTNER");
		eq(LOAN_SYSTEM_USER.NAME, searchRequest.getUserName());
		
		 return search(pageable, record -> {
			 GalaxyCardPromoteChannelUserRightResponse response = new GalaxyCardPromoteChannelUserRightResponse();
			 	response.setUserId(record.get(LOAN_SYSTEM_USER.ID));
	            response.setUserName(record.get(LOAN_SYSTEM_USER.NAME));
	            response.setRoleName(record.get(LOAN_ROLE.NAME));
	            return response;
	        });
		
		
	}

}
